<?php
namespace app\agent\controller;

use app\agent\service\UserAgentService;
use app\common\controller\Basic;
use app\common\validate\UserAgentValidate;
use think\Url;

/**
 * 代理商后台管理
 * @module 代理商后台管理
 * @controller 代理商后台
 */
class LoginController extends Basic
{
	private $userAgent = null;

	private $validate = null;
	
	public function __construct()
	{
		parent::__construct();

		$action = [
		    'logout',
            'freelogin'
        ];
        if ( empty(session('uid')) === false && in_array(request()->action(), $action) === false )
        {
            $this->redirect('Index/index');
        }
		
		$this->userAgent = new UserAgentService();

		$this->validate = new UserAgentValidate();
	}

    public function login()
    {
        $this->isPost();

        return $this->fetch('login');
	}

    public function loginpost()
    {
        $request = input('post.');

        //验证数据
        if( $this->validate->scene('login')->check($request) === false )
        {
            $this->failure('登录失败:' . $this->validate->getError());
        }

        //验证登录
        if( $this->userAgent->login($request) === false )
        {
            $this->failure('登录失败:' . $this->userAgent->getError());
        }

        $this->render('登录成功', url('index/index'));
	}

    /**
     * 登出
     */
    public function logout()
    {
        session(null, 'agent');

        $this->redirect('login');
	}

    /**
     * @action 免密登录
     */
    public function freeLogin()
    {
        $request = input('param.');

        //验证
        if( $this->validate->scene('freeLogin')->check($request) === false )
        {
            $this->error($this->validate->getError());
        }

        if( $this->userAgent->freeLogin($request) === false )
        {
            $this->error($this->userAgent->getError());
        }

        $this->redirect('index/index');
	}


	
}